<?php
/*
 CALENDAR.INC.PHP by martin $ 2009/04/15 01:35:05
*/

$prev = date('Ym', mktime(0,0,0,$D['mon'],0,$D['year'])); 
$next = date('Ym', mktime(0,0,0,$D['mon']+1,1,$D['year'])); 
$_CALENDAR_LANG = isset($_CALENDAR_LANG) ? $_CALENDAR_LANG : 1;
$wName = $_CALENDAR_LANG ? array('日', '月', '火', '水', '木', '金', '土') : array('S', 'M', 'T', 'W', 'T', 'F', 'S');

function selectMonth($url="index.php"){  // 月のセレクトメニュー
 global $D, $_CALENDAR_LANG;
 $fullmonth = $_CALENDAR_LANG == 1 ?
  array(1=>'1月','2月','3月','4月','5月','6月','7月','8月','9月','10月','11月','12月') :
  array(1=>'January','February','March','April','May','June','July','August','September','October','November','December');
 $s = "   <select onchange=\"window.location=(oParts.baseURL+this.value)\">\n";
 foreach($fullmonth as $i=>$value){
  $link = MODE_STATIC ? $D['year'].'/'.sprintf('%02d', $i).'/' : '?date='.$D['year'].sprintf('%02d', $i);
  $s .= '    <option value="'.$link.'"';
  $s .= ($D['mon']==$i) ?  ' selected="selected"' : '';
  $s .= ">$value</option>\n";
 }
 $s .= "   </select>\n";
 return $s;
}
function selectYear($url="index.php"){  // 西暦のセレクトメニュー
 global $D, $LOGS, $_CALENDAR_LANG;
 $LOGS = array_reverse($LOGS);
 $s = '   <select onchange="window.location=(oParts.baseURL+this.value);">'.NL;
 foreach ($LOGS as $i => $log){
  $year = substr($log, 4, 4);
  if(!is_numeric($year)) continue;
  if($i > 0){
   if($year == substr($LOGS[$i - 1], 4, 4)) continue;
  }
  $link = MODE_STATIC ? date('Y/', strtotime("$year/01/01")) : "$url?date=$year";
  $s .= '    <option value="'.$link.sprintf('%02d', $D['mon']).(MODE_STATIC ? '/' : '').'"';
  $s .= ($D['year'] == $year) ?  ' selected="selected"' : '';
  $s .= '> '.$year.($_CALENDAR_LANG ? '年' : '').' </option>'.NL;
 }
 $s .= "   </select>\n";
 return $s;
}

function selectForm($month, $year, $lang){
 $h = '<form action="index.php" method="post">
   <fieldset>
 ';
 if($lang){ // ja
  $h .= selectYear();
  $h .= selectMonth();
 } else {
  $h .= selectMonth();
  $h .= selectYear(); 
 }
 $h .= '  </fieldset>
 </form>';
 return $h;
}

function TableCalendar($month, $year, $type='box', $start=0, $selector=1, $lang=1, $forced=0){  // by TABLE element
 global $prev, $next, $theme_dir, $D, $_self, $hd, $wName;
 if(is_file(log_($D[0]))){
  $LINES = my_file(log_($D[0]));
 } else $LINES = array();
 $select = selectForm($month, $year, $lang);
 if($type == 'box'){
  if($selector){
  $h = '
 <div class="calendar-top select-type">
  '.$select.'
 </div><!-- #calendar-top -->'.NL;
  } else {
   $pre = MODE_STATIC ? ROOT_PATH.substr($prev, 0, 4).'/'.substr($prev, 4, 2).'/' : "index.php?date=$prev";
   $nxt = MODE_STATIC ? ROOT_PATH.substr($next, 0, 4).'/'.substr($next, 4, 2).'/' : "index.php?date=$next";
   $h = '
  <div class="calendar-top">
   <div class="prev-month"><a href="'.$pre.'"><img src="'.$theme_dir.'Images/prev.png" alt="prev" title="先月を表示" /></a></div>
   <div class="calendar-cap">'.$year.'.'.$month.'</div>
   <div class="next-month"><a href="'.$nxt.'"><img src="'.$theme_dir.'Images/next.png" alt="next" title="来月を表示" /></a></div>
  </div><!-- #calendar-top -->'.NL;
  }
 } else $h = '';
 
 if($month < 1){
  $month = 12 - $month;
  $year--;
 }elseif($month > 12){
  $month = $month - 12;
  $year++;
 }

 $now = time(); $day = 1;
 $dayFirst = getdate(mktime(0, 0, 0, $month, $day, $year));
 $wday = $dayFirst['wday'];
 $wday = ($wday + 7 - $start) % 7;
 $h .= '
 <div class="calendar-body">
  <table summary="月間カレンダー">'.NL;
 if($type == 'box'){
  $h .= '   <thead><tr>';
  for ($j = $start; $j < ($start + 7); $j++){
   $h .= '<th>'.$wName[$j % 7].'</th>';
  }
  $h .= '</tr></thead>'.NL;
  $h .= '   <tbody>'.NL.'   <tr>'.NL;
  for($i = 0; $i < $wday; $i++){  // first week
   $h .= '    <td class="blank">&nbsp;</td>'.NL;
  }
 } else {
  $h .= '   <tbody>'.NL.'   <tr>'.NL;
  if($selector){
   $h .= '    <td>'.$select.'</td>'.NL;
  } else {
   $pre = MODE_STATIC ? ROOT_PATH.substr($prev, 0, 4).'/'.substr($prev, 4, 2).'/' : "index.php?date=$prev";
   $nxt = MODE_STATIC ? ROOT_PATH.substr($next, 0, 4).'/'.substr($next, 4, 2).'/' : "index.php?date=$next";
   $h .= '    <td class="prev-month"><a href="'.$pre.'"><img src="'.$theme_dir.'Images/prev.png" alt="prev" title="先月を表示" /></a></td>
    <td class="calendar-cap">'.$year.'.'.$month.'</td>
    <td class="next-month"><a href="'.$nxt.'"><img src="'.$theme_dir.'Images/next.png" alt="next" title="来月を表示" /></a></td>
';
  }
 }
 $mark = '';
 while(checkdate($month, $day, $year)){
  $link = sprintf("%4d%02d%02d", $year, $month, $day);
  $TD = ($day==date('j', $now) && $month==date('n', $now) && $year==date('Y', $now)) ? ' Today' : '';
  $Sun = ($start + $wday) % 7 == 0 ? 'Sun' : '';
  $Sat = ($start + $wday) % 7 == 6 ? 'Sat' : '';
  $class = '';
  if(isset($hd->holidays[$link])){
   $HD = array(' Holiday', NL.' '.$hd->holidays[$link]);
   $class = $Sat.$Sun.$HD[0].$TD.' ';
  } else {
   $HD = '';
   $class = $Sat.$Sun.$TD.' ';
  }
  
  $utime = strtotime($link);
  if(!empty($LINES)){
   foreach($LINES as $i=>$value){
    list($id,,$title,) = explode('|', $value);
    if(strstr($id, 'd')) continue;
    if(strpos($title, ',')) list($title, ) = explode(',', $title);
    if(date('Ymd', $id)==$link){
     $mark = MODE_STATIC ? ROOT_PATH.substr($link, 0, 4).'/'.substr($link, 4, 2).'/'.substr($link, 6, 2).'/' :
      "index.php?mode=show&amp;date=".$link;
     if($i < count($LINES)-1){
      list($next_id,) = explode('|', $LINES[$i+1]);
     } else $next_id = '';
     $others = ($next_id!='' && date('Ymd', substr($next_id,0,10))==$link) ? ' etc.' : '';
     $h .= '    <td class="'.(trim($class) != '' ? $class : '').'daySubmit" title="'.$title.$others.($HD ? $HD[1] : '').'">';
     break;
    } else $mark = '';
   }
  } else $mark = '';
  if($mark != ''){
   $h .= "<a href=\"$mark\" class=\"event\">$day</a></td>".NL;
  } else {
   if(trim($class) != ''){
    $h .= '    <td class="'.trim($class).'"'.($HD ? ' title="'.trim($HD[1]).'"' : '').'>'.$day.'</td>'.NL;
   } else $h .= "    <td>$day</td>".NL;
  }
  $day++;
  $wday++;
  if(($wday % 7) == 0){
   if($type == 'box') $h .= '   </tr>'.NL.'   <tr>'.NL;
   $wday = 0;
  }
 }
 if($type == 'box'){
  while($wday < 7 && $wday != 0) { // Blank
   $h .= '    <td class="blank">&nbsp;</td>'.NL;
   $wday++;
  }
 }
 $h .= '   </tr>'.NL.'   </tbody>'.NL.'  </table>
 </div><!--#calendar-body-->
 ';
 if($type == 'box'){
  $h .= '<div class="calendar-bottom">&nbsp;</div>'.NL;
 }
 $calendar = rtrim($h);
 $Ym = date('Ym',$D[0]);
 // キャッシュファイルにしておく
 if($type == 'box'){
  if($selector){
   if(!is_file($f=DB.$Ym.'_box2.db') || $forced){
    if( abs(time()-$D[0]) < 15552000 ) rewrite($f, $calendar); // 前後6ヶ月
   }                        
  } else {
   if(!is_file($f=DB.$Ym.'_box1.db') || $forced) {
    if( abs(time()-$D[0]) < 15552000 ) rewrite($f, $calendar); // 前後6ヶ月
   }
  }
 } else {
  if($selector){
   if(!is_file($f=DB.$Ym.'_line2.db') || $forced){
    if( abs(time()-$D[0]) < 15552000 ) rewrite($f, $calendar); // 前後6ヶ月
   }                        
  } else {
   if(!is_file($f=DB.$Ym.'_line1.db') || $forced) {
    if( abs(time()-$D[0]) < 15552000 ) rewrite($f, $calendar); // 前後6ヶ月
   }
  }
 }
 return $calendar;
}


function ListCalendar($month, $year, $start=0, $selector=1, $lang=1, $forced=0){  // このスクリプトのキモ
 # $forced: true ならキャッシュの更新
 global $prev, $next, $D, $_self, $hd, $wName, $theme_dir;
 if(is_file(log_($D[0]))){
  $LINES = my_file(log_($D[0]));
 } else $LINES = array();
 $select = selectForm($month, $year, $lang);
 if($selector){
  $h = '
 <div class="calendar-top select-type">
  '.$select.'
 </div><!-- #calendar-top -->'.NL;
 } else {
  $pre = MODE_STATIC ? ROOT_PATH.substr($prev, 0, 4).'/'.substr($prev, 4, 2).'/' : "index.php?date=$prev";
  $nxt = MODE_STATIC ? ROOT_PATH.substr($next, 0, 4).'/'.substr($next, 4, 2).'/' : "index.php?date=$next";
  $h = '
  <div class="calendar-top">
   <div class="prev-month"><a href="'.$pre.'"><img src="'.$theme_dir.'Images/prev.png" alt="prev" title="先月を表示" /></a></div>
   <div class="calendar-cap">'.$year.'.'.$month.'</div>
   <div class="next-month"><a href="'.$nxt.'"><img src="'.$theme_dir.'Images/next.png" alt="next" title="来月を表示" /></a></div>
  </div>
';
 }
 
 if($month < 1){
  $month = 12 - $month;
  $year--;
 }elseif($month > 12){
  $month = $month - 12;
  $year++;
 }
 $now = time(); $day = 1;
 $dayFirst = getdate(mktime(0, 0, 0, $month, $day, $year));
 $wday = $dayFirst['wday'];
 $wday = ($wday + 7 - $start) % 7;
 $h .= '
 <div class="calendar-body">
  <ol class="wheader">'.NL;
 for ($j = $start; $j < ($start + 7); $j++){
  $h .= '   <li>'.$wName[$j % 7].'</li>'.NL;
 }
 $h .= '  </ol>'.NL.'  <ol class="weekday">'.NL;
 for($i = 0; $i < $wday; $i++){  // first week
  $h .= '   <li class="blank">&nbsp;</li>'.NL;
 }
 $mark = '';
 while(checkdate($month, $day, $year)){
  $link = sprintf("%4d%02d%02d", $year, $month, $day);
  $TD = ($day==date('j', $now) && $month==date('n', $now) && $year==date('Y', $now)) ? ' Today' : '';
  $Sun = ($start + $wday) % 7 == 0 ? 'Sun' : '';
  $Sat = ($start + $wday) % 7 == 6 ? 'Sat' : '';
  $class = '';
  if(isset($hd->holidays[$link])){
   $HD = array(' Holiday', NL.' '.$hd->holidays[$link]);
   $class = $Sat.$Sun.$HD[0].$TD.' ';
  } else {
   $HD = '';
   $class = $Sat.$Sun.$TD.' ';
  }
  
  $utime = strtotime($link);
  if(!empty($LINES)){
   foreach($LINES as $i=>$value){
    list($id,,$title,) = explode('|', $value);
    if(strstr($id, 'd')) continue;
    if(strpos($title, ',')) list($title, ) = explode(',', $title);
    if(date('Ymd', $id)==$link){
     $mark = MODE_STATIC ? ROOT_PATH.substr($link, 0, 4).'/'.substr($link, 4, 2).'/'.substr($link, 6, 2).'/' :
      "index.php?mode=show&amp;date=".$link;
     if($i < count($LINES)-1){
      list($next_id,) = explode('|', $LINES[$i+1]);
     } else $next_id = '';
     $others = ($next_id!='' && date('Ymd', substr($next_id,0,10))==$link) ? ' etc.' : '';
     $h .= '   <li class="'.(trim($class) != '' ? $class : '').'daySubmit" title="'.$title.$others.($HD ? $HD[1] : '').'">';
     break;
    } else $mark = '';
   }
  } else $mark = '';
  if($mark != ''){
   $h .= "<a href=\"$mark\" class=\"event\">$day</a></li>".NL;
  } else {
   if(trim($class) != ''){
    if(trim($class) == 'Today') $h .= '   <li class="Today" title="今日">'.$day.'</li>'.NL;
    else $h .= '   <li class="'.trim($class).'"'.($HD ? ' title="'.trim($HD[1]).'"' : '').'>'.$day.'</li>'.NL;
   } else $h .= "   <li>$day</li>".NL;
  }
  $day++;
  $wday++;
  if(($wday % 7) == 0) $wday = 0;
 }
 while($wday < 7 && $wday != 0) { // Blank
   $h .= '   <li class="blank">&nbsp;</li>'.NL;
   $wday++;
 }
 $h .= '  </ol>
  </div><!--#calendar-body-->
  <div class="calendar-bottom">&nbsp;</div>'.NL;
 $calendar = rtrim($h);
 $Ym = date('Ym',$D[0]);
 // キャッシュファイルにしておく
 if($selector){
  if(!is_file($f=DB.$Ym.'_OL2.db') || $forced){
   if( abs(time()-$D[0]) < 15552000 ) rewrite($f, $calendar); // 前後6ヶ月
  }                        
 } else {
  if(!is_file($f=DB.$Ym.'_OL1.db') || $forced) {
   if( abs(time()-$D[0]) < 15552000 ) rewrite($f, $calendar); // 前後6ヶ月
  }
 }
 return $calendar;
}

?>
